package com.groot.medical.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.ChronicDisease;
import com.groot.medical.entity.ChronicPolicy;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.PaySetting;
import com.groot.medical.service.ChronicPolicyService;
import com.groot.medical.service.ChronicPolicyServiceImpl;

@WebServlet("/sys/policy.do")
public class ChronicPolicyController extends BaseServletController{
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(ChronicPolicyController.class);
	private ChronicPolicyService chronicPolicyService = new ChronicPolicyServiceImpl();
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	
	public String findChronicPolicyInfoByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		int pageSize = 2;
	    PageBean areaPage = chronicPolicyService.findChronicPolicyInfoByPage(pageNum, pageSize);
        request.setAttribute("areaPage", areaPage);
		
		
		return "/WEB-INF/page/policy/policy_list.jsp";
	}
	/**
	 * 打开添加页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String toAddChronicPolicyInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setAttribute("tgId", request.getParameter("tgId"));
		
		return "/WEB-INF/page/policy/policy_add.jsp";
	}
	/**
	 * 添加参合缴费设置信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addChronicPolicyInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String money = request.getParameter("money");
		String scale = request.getParameter("scale");
		
		
		//生成时间
		Date now = new Date();
		SimpleDateFormat year = new SimpleDateFormat("yyyy");//设置日期格式
		String nowTime = year.format(now);
		
		//封装
		ChronicPolicy policy = new ChronicPolicy();
		policy.setYear(nowTime);
		policy.setMoney(money);
		policy.setScale(Float.parseFloat(scale));
		//执行添加
		chronicPolicyService.addChronicPolicyInfo(policy);
		request.setAttribute("tgId",request.getParameter("tgId"));
		request.setAttribute("pageNum",request.getParameter("pageNum"));
		
		return findChronicPolicyInfoByPage(request,response);
	}	
	/**
	 * 打开修改慢性病政策页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String toEditChronicPolicyInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String id = request.getParameter("id");
		//查询待修改信息
		ChronicPolicy policy=chronicPolicyService.findChronicPolicyInfoById(id);
		request.setAttribute("tgId", request.getParameter("tgId"));
		request.setAttribute("policy", policy);
		return "/WEB-INF/page/policy/policy_edit.jsp";
	}
	/**
	 * 修改慢性病政策
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editChronicPolicyInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String money = request.getParameter("money");
		String scale = request.getParameter("scale");
		String year = request.getParameter("year");
		String id = request.getParameter("id");
		
		ChronicPolicy policy = new ChronicPolicy();
		policy.setId(Integer.valueOf(id));
		policy.setYear(year);
		policy.setMoney(money);
		policy.setScale(Float.parseFloat(scale));
		//执行修改方法
		chronicPolicyService.editChronicPolicyInfo(policy);
		return findChronicPolicyInfoByPage(request,response);
	}
}
